<template>
	<view>
		<view class="×" @click="huitui">×</view>
		<view class="user">
			<text class="title">登录</text>
			<wd-input class="Input" v-model="tel" prefix-icon="phone" @change="handleChange" placeholder="请输入手机号" />
			<wd-input class="Input" v-model="password" prefix-icon="lock-off" clearable show-password @change="handleChange" placeholder="请输入密码" />
			<view class="forget" @click="tiaozhuan('忘记')">忘记密码?</view>
			<button class="login_Button" @click="login">登录</button>
			<text>
				登录即表示您已详细阅读并同意
				<text style="color: yellowgreen">《用户服务协议》</text>
				和
				<text style="color: yellowgreen">《用户隐私协议》</text>
			</text>
			<!-- 			<button class="wechat_login">
				<uni-icons type="weixin" size="20"></uni-icons>
				微信登录
			</button> -->
			<text class="enroll" @click="tiaozhuan('注册')">注册</text>
			<wd-toast />
		</view>
	</view>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Loginuser } from '../../api/api';
import { useToast } from '@/uni_modules/wot-design-uni';

const toast = useToast();

let tel = ref('');
let password = ref('');

// 页面跳转
const tiaozhuan = (item) => {
	if (item == '注册') {
		uni.navigateTo({
			url: '/pages/enroll/enroll'
		});
	} else {
		uni.navigateTo({
			url: '/pages/forget/forget'
		});
	}
};
// 回跳
const huitui = () => {
	uni.redirectTo({
		url: '/pages/Home/Home'
	});
};
// 登录
const login = async () => {
	if (tel.value.length > 0 && password.value.length > 0) {
		let data = await Loginuser({ tel: tel.value, password: password.value });
		if (data.code == 200) {
			localStorage.setItem('access_token', data.data.access_token);
			localStorage.setItem('REFRESH_token', data.data.REFRESH_token);
			localStorage.setItem('_id', data._id);
			toast.success({
				iconClass: 'star',
				msg: '登录成功'
			});
			let a = setTimeout(() => {
				uni.redirectTo({
					url: '/pages/Home/Home'
				});
				clearTimeout(a);
			}, 2000);
		} else {
			toast.error('登录失败，账号密码有误');
		}
	} else if (tel.value.length <= 0 && password.value.length > 0) {
		toast.error('手机号不能为空');
	} else if (tel.value.length > 0 && password.value.length <= 0) {
		toast.error('请输入密码');
	} else if (tel.value.length <= 0 && password.value.length <= 0) {
		toast.error('请输入账户密码');
	}
};
</script>

<style lang="scss" scoped>
.× {
	width: 60px;
	height: 60px;
	font-size: 60rpx;
	text-align: center;
	line-height: 60px;
}
.user {
	margin: 20px;
	position: relative;
	.title {
		display: block;
		font-size: 40px;
		margin-top: 100px;
		margin-bottom: 60px;
	}
	.Input {
		margin-bottom: 50rpx;
	}
	.forget {
		position: absolute;
		right: 0;
	}
	.login_Button {
		margin-top: 80px;
		background-color: yellow;
		margin-bottom: 20px;
	}
	.enroll {
		position: fixed;
		left: 50%;
		bottom: 10%;
		color: black;
		margin-left: -16px;
	}
	.wechat_login {
		position: fixed;
		left: 50%;
		bottom: 15%;
		margin-left: -5rem;
		width: 10rem;
		background-color: white;
		border: 1px solid gray;
		color: gray;
		border-radius: 50px;
	}
}
</style>